<?php

 defined('_JEXEC') or die('Access denied');
 jimport('joomla.application.component.model');

 class ScorecardModelScoreCard extends JModel {
 	var $_courseid = null;
 	var $_scorecard = null;
 	var $_holes = null;
 	var $_scores = null;

	var $_scoreid = null;
	var $_score = null;

 	function __construct() {
 		parent::__construct();

 		$courseid = JRequest::getVar('courseid', 0, '', 'int');
 		$this->_courseid = $courseid;
 		$this->_scores = JRequest::getVar('scores', array(), 'post', 'array');
 		$this->_scoreid = JRequest::getVar('scoreid', 0, '', 'int');
 	}

 	function getScoreId() {
 		return $this->_scoreid;
 	}

 	function getRelatedIds() {
 		if ($this->_scoreid) {
 			$query = "SELECT C.id as courseid, C.countryid, C.stateid FROM #__golfcourses C, #__golferscores S WHERE C.id = S.courseid AND S.id='" . $this->_scoreid . "'";
 			$this->_db->setQuery($query);
 			return $this->_db->loadAssoc();
 		}
 		return array();
 	}

 	function getScorecard($id = null) {
 		if (!$this->_scorecard) {
 			// Get scorecard data
 			$query = "SELECT S.* FROM #__golfcourses C, #__scorecards S
					  WHERE C.scorecardid = S.id
					  AND C.id = '" . ($id ? $id : $this->_courseid) . "'";
			$this->_db->setQuery($query);
			$this->_scorecard = $this->_db->loadObject();
 		}
 		if (!$this->_holes) {
	 		$query = "SELECT H.* FROM #__golfcourses C, #__scorecards S, #__scorecard_holes H
					  WHERE C.scorecardid = S.id
					  AND H.scorecardid = S.id
					  AND C.id = '" . ($id ? $id : $this->_courseid) . "'";
			$this->_holes = $this->_getList($query, 0, 0);
 		}
 		return array('scorecard' => $this->_scorecard, 'holes' => $this->_holes);
 	}

 	function getScore($id = null) {
 		$usergender = $this->getUserGender();

 		if (!$this->_score) {
 			$query = " SELECT S.*, SUM(IF(H.number <=9, H.score, 0)) as ins, SUM(IF(H.number > 9, H.score, 0)) AS outs FROM #__golferscores S, #__score_holes H " .
 					 " WHERE S.id = '" . ($id ? $id : $this->_scoreid) . "' " .
 					 " AND S.id = H.scoreid " .
 					 " GROUP BY S.id";
 			$this->_db->setQuery($query);
 			$this->_score['scoreinfo'] = $this->_db->loadAssoc();

 			$query = "SELECT * FROM #__score_holes WHERE scoreid = '" . ($id ? $id : $this->_scoreid) . "'";
 			$this->_db->setQuery($query);
 			if ($usergender == 1) {
 				$this->_score['scores']['player2'] = $this->_db->loadAssocList();
 			} else {
 				$this->_score['scores']['player1'] = $this->_db->loadAssocList();
 			}
 		}
 		return $this->_score;
 	}

 	function getUserGender() {
 		$user =& JFactory::getUser();
 		$query = "SELECT gender FROM #__user_extra_info WHERE userid = '" . $user->id . "'";
 		$this->_db->setQuery($query);
 		$gender = $this->_db->loadResult();
 		return $gender;
 	}

 	function setUserGender($gender) {
 		$user =& JFactory::getUser();
 		$query = "INSERT INTO #__user_extra_info(userid, gender) VALUES('" . $user->id . "','" . $gender . "')";
 		$this->_db->setQuery($query);
 		$this->_db->query();
 		return $this->_db->insertid() > 0;
 	}

 	function saveScorecard() {
 		if ($this->_scores && $this->_courseid) {
 			jimport('joomla.utilities.date');

 			// Create the score record
 			$score =& JTable::getInstance('golferscore', 'Table');

			// Get user gender
			$gender = $this->getUserGender();

			// If there is no information about user gender then insert new
			if ($gender === null) {
				$gender = JRequest::getVar('usergender');
				$this->setUserGender($gender);
			}

			$womenscore = ($gender == 1);

 			$post = JRequest::get('post');
 			$user =& JFactory::getUser();
 			$player1 =& $this->_scores['player1'];
 			$player2 =& $this->_scores['player2'];


 			$post['userid'] = $user->id;
 			$post['courseid'] = $this->_courseid;
			$date = new JDate($post['playdate']);
			$post['playdate'] = $date->toMySQL();

 			$holes = $womenscore ? $player2 : $player1;

			// Save the score record first
			if (!$score->bind($post)) {
				return false;
 			}
			if ($post['scoreid']) {
 				$score->id = $post['scoreid'];
 			}

 			foreach ($holes as $num => $row) {
 				$sc = (int) $row['score'];
 				if ($num <= 9) {
 					$ins += $sc;
 				} else {
 					$outs += $sc;
 				}
 				$total += $sc;
 			}

 			if (!$score->store()) {
 				return false;
 			}

 			$scoreid = $score->id;
 			foreach ($holes as $num => $row) {
				$hole =& JTable::getInstance('scorehole', 'Table');
				$data['scoreid'] = $scoreid;
				$data['number'] = $num;
				$data['score'] = $row['score'];
				$data['id'] = $row['id'];
				if (!$hole->bind($data) || !$hole->store()) {
					continue;
				}
 			}
 			return true;
 		}
 	}
 }
?>
